<?php 
class activityModel extends Model{
		public function activityoverviewAction(){
				
				$uid = $_SESSION['user']['id'];
				$checkone = "SELECT a.id,a.activityName,a.activityContent,a.activityDate,a.num,a.activityLimit,a.lastDate,a.img,u.avatar,u.username FROM activity a LEFT JOIN `user` u ON a.uid = u.id WHERE a.id in (SELECT activityId FROM participants WHERE parId = $uid)";
				//查询过程
					$dataone = $this->link->selectDB($checkone);
				//查询语句
					$checktwo = "SELECT a.id,a.activityName,a.activityContent,a.activityDate,a.num,a.activityLimit,a.lastDate,a.img,u.avatar,u.username FROM activity a LEFT JOIN `user` u ON a.uid = u.id WHERE u.id = $uid and a.status= 1";
				//查询过程
					$datatwo = $this->link->selectDB($checktwo);
					
					
				//返回数据

				 echo json_encode(['one'=>$dataone,'two'=>$datatwo]);
							
		}
		public function activitydetailsAction(){
				$activityid = $_GET['activityid'];
				//查询语句
					$checkone = "SELECT a.id,a.activityName,a.activityContent,a.activityDate,a.activityNum,a.num,a.activityLimit,a.lastDate,a.img,a.uid,u.avatar,u.username FROM activity a LEFT JOIN `user` u ON a.uid = u.id WHERE a.id = $activityid";
				//查询过程
					$dataone = $this->link->selectDB($checkone);
				//查询语句
					$checktwo = "SELECT * from `user` WHERE id in (SELECT parId from participants WHERE activityId =$activityid)";
				//查询过程
					$datatwo = $this->link->selectDB($checktwo);
					
				//返回数据
					echo json_encode(['one'=>$dataone,'two'=>$datatwo]);
							
		}
		public function joinAction(){
				$time = time();
				$uid = $_SESSION['user']['id'];
				$releaseid = $_GET['releaseid'];
				$sexlimit = $_GET['sexlimit'];
				$activitylastDate = $_GET['activitylastDate'];
				$activitylgnum = $_GET['activitylgnum'];
				$activitysnum = $_GET['activitysnum'];
				$lasttime = strtotime($activitylastDate);
				if($activitylgnum>$activitysnum){
					if($time<$lasttime){
						if($sexlimit==0){
							$checkone = "SELECT * from participants WHERE activityId=$releaseid AND parId =$uid";
							$resultone = $this->link->selectRows($checkone);
							if($resultone!=0){
								echo json_encode(['i' => '已参加，请勿重复参加','msg' =>'201']);
							}else{
								$checktwo = "INSERT INTO participants(activityId, parId) VALUES ($releaseid,$uid)";
								$resulttwo = $this->link->query($checktwo);
								if($resulttwo){
									
									$checkthree = "UPDATE activity a SET a.num = a.num+1  WHERE id = $releaseid";
										
									$resultthree = $this->link->query($checkthree);
									if($resultthree){
										echo json_encode(['i' => '参加成功','msg' =>'200']);
									}else{
										echo json_encode(['i' => '未知错误','msg' =>'202']);
									}
								}else{
									echo json_encode(['i' => '未知错误','msg' =>'203']);
								}
							}
						}else if($sexlimit==1){
							$checkZero = "SELECT sex from `user` where id=$uid";
							$resultZero = $this->link->selectDB($checkZero);
							if($resultZero[0]['sex']=='男'){
								$checkone = "SELECT * from participants WHERE activityId=$releaseid AND parId =$uid";
								$resultone = $this->link->selectRows($checkone);
								if($resultone!=0){
									echo json_encode(['i' => '已参加，请勿重复参加','msg' =>'201']);
								}else{
									$checktwo = "INSERT INTO participants(activityId, parId) VALUES ($releaseid,$uid)";
									$resulttwo = $this->link->query($checktwo);
									if($resulttwo){
										
										$checkthree = "UPDATE activity a SET a.num = a.num+1  WHERE id = $releaseid";
											
										$resultthree = $this->link->query($checkthree);
										if($resultthree){
											echo json_encode(['i' => '参加成功','msg' =>'200']);
										}else{
											echo json_encode(['i' => '未知错误','msg' =>'202']);
										}
									}else{
										echo json_encode(['i' => '未知错误','msg' =>'203']);
									}
								}
							}else{
								echo json_encode(['i' => '因为性别,被限制参加','msg' =>'204']);
							}
						}else if($sexlimit==2){
							$checkZero = "SELECT sex from `user` where id=$uid";
							$resultZero = $this->link->selectDB($checkZero);
							if($resultZero[0]['sex']=='女'){
								$checkone = "SELECT * from participants WHERE activityId=$releaseid AND parId =$uid";
								$resultone = $this->link->selectRows($checkone);
								if($resultone!=0){
									echo json_encode(['i' => '已参加，请勿重复参加','msg' =>'201']);
								}else{
									$checktwo = "INSERT INTO participants(activityId, parId) VALUES ($releaseid,$uid)";
									$resulttwo = $this->link->query($checktwo);
									if($resulttwo){
										
										$checkthree = "UPDATE activity a SET a.num = a.num+1  WHERE id = $releaseid";
											
										$resultthree = $this->link->query($checkthree);
										if($resultthree){
											echo json_encode(['i' => '参加成功','msg' =>'200']);
										}else{
											echo json_encode(['i' => '未知错误','msg' =>'202']);
										}
									}else{
										echo json_encode(['i' => '未知错误','msg' =>'203']);
									}
								}
							}else{
								echo json_encode(['i' => '因为性别,被限制参加','msg' =>'205']);
							}
						}
					}else{
						echo json_encode(['i' => '活动已结束','msg' =>'206']);
					}
				}else{
					echo json_encode(['i' => '活动可参与人数已满','msg' =>'207']);
				}
				
		}
		
	}
	?>